package com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.impl;

import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.entity.LossArchitecture;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.entity.LossStructure;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.EarthquakeDisasterLossRepository;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/02/10
 * @Description: 建（构）筑物地震灾害损失报表
 */
@Repository
public class EarthquakeDisasterLossRepositoryImpl implements EarthquakeDisasterLossRepository {
    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private static final String ARCHITECTURE = "xczh_loss_architecture"; // 表名
    private static final String STRUCTURE = "xczh_loss_structure"; // 表名

    @Override
    public void deleteByXlrmId(String xlrmId) {
        String sql = "delete from  "+ARCHITECTURE+"  where xlrm_id= '"+xlrmId+"'" ;
        String sql2 = "delete from  "+STRUCTURE+"  where xlrm_id= '"+xlrmId+"'" ;
        jdbcTemplate.update(sql);
        jdbcTemplate.update(sql2);
    }

    @Override
    public void batchArchitectureSave(List<LossArchitecture> list) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(list.toArray());
        String sql = "INSERT INTO "+ARCHITECTURE +
                " ( id,xlrm_id,fill_form_user,tel,fill_form_time,province,city,county,village," +
                "classification,total_quantity,civil_engineering,decoration," +
                "good,minor_damage,moderate_damage,severe_damage,destroy,economic_loss,sequence)" +
                "values (:id,:xlrmId,:fillFormUser,:tel,:fillFormTime,:province,:city,:county,:village," +
                ":classification,:totalQuantity,:civilEngineering,:decoration," +
                ":good,:minorDamage,:moderateDamage,:severeDamage,:destroy,:economicLoss,:sequence)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public void batchStructureSave(List<LossStructure> list) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(list.toArray());
        String sql = "INSERT INTO "+STRUCTURE +
                " ( id,xlrm_id,fill_form_user,tel,fill_form_time,province,city,county,village," +
                "classification,total_quantity,average_cost,unit_of_measurement,failure_desc,economic_loss,sequence)" +
                "values (:id,:xlrmId,:fillFormUser,:tel,:fillFormTime,:province,:city,:county,:village," +
                ":classification,:totalQuantity,:averageCost,:unitOfMeasurement,:failureDesc,:economicLoss,:sequence)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }
}
